﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel;
using DAL;
using System.Collections;
using System.Web.Mvc;
using System.Web;

namespace BIZ
{
    public  class cmsOper
    {
        /// <summary>
        /// 检查权限
        /// </summary>
        /// <param name="right">权限代码</param>
        /// <returns>是否有权限</returns>
        public static bool IsHavePower(string right)
        {
            bool flag = false;
            cmsuser user = (cmsuser)System.Web.HttpContext.Current.Session["cmsuser"];
            string power = string.Empty;
            if (user != null)
            {
                power = user.sectionCodeList.ToString().Trim();
            }
            if (string.IsNullOrEmpty(power) || Array.IndexOf(power.Split(','),right) > -1)
            {
                flag = true; 
            }
            return flag;
        }

        /// <summary>
        /// 检查用户权限(若没有该权限,则跳转至提示页)
        /// </summary>
        /// <param name="checkRights">当前页面对应的权限代码</param>
        public static void CheckUserRights(string rightCode)
        {
            if (!IsHavePower(rightCode))
            {
                string url = "/Share/norright.html";
                HttpContext.Current.Response.Redirect(url, true);
            }
        }

        public static cmsuser GetCmsUser(Operator oper)
        {
            cmsuser user = new cmsuser
            {
                id = oper.O_id,
                pwd = oper.O_Password,
                workid = oper.O_Workid,
                remark = oper.O_Remark,
                rid = oper.O_Rid
            };            

            if (oper.O_Rid != 0)
            {
                RoleOper roleOper = new RoleOper();
                IList list = roleOper.getRolebyid(oper.O_Rid);
                if (list.Count > 0)
                {
                    Hashtable h = (Hashtable)list[0];
                    user.sectionCodeList = h["R_SectionCodeList"].ToString();
                }                
            }
            return user;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="workid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static IList login(string workid, string pwd)
        {
            return OperatorOper.getOperator(workid, pwd);
        }
    }
}
